﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;
using BusinessLayer;
using BoletoElectronicoDesktop.AbmBeneficiarios;

namespace BoletoElectronicoDesktop.AbmPost_Net
{
    public partial class FormPostnetList : Form
    {
        private Util.Funcionalidad uso;

        //id de los postnets a ignorar
        private List<int> postnetsToIgnore;

        public FormPostnetList(Util.Funcionalidad use)
        {
            InitializeComponent();
            this.uso = use;
            if(Util.Funcionalidad.PostNetSelectOne.Equals(use))
            {
                //Permite seleccionar solo un elemento
                dgvPostnet.Columns[3].Visible = false;
                btnAceptar.Visible = false;
            }else
            {
                //Permite seleccionar al menos un elemento
                dgvPostnet.Columns[4].Visible = false;
            }
        }

        public void btnBuscar_Click(object sender, EventArgs e)
        {
            if (Util.Funcionalidad.PostNetSelectOne.Equals(uso))
            {
                retrieveAllPostnet();
            }
            else
            {
                retrievePostnetDisponible();
            }
        }

        private void retrievePostnetDisponible()
        {
            int nroSerie = obtenerNumerodeSerie();
            dsBoleto.PostnetDataTable dt = BizPostnet.getPostnetDisponibles(txtMarca.Text, txtModelo.Text, nroSerie, postnetsToIgnore);
            if (dt.Count != 0)
            {
                this.dgvPostnet.DataSource = dt;
            }
            else
            {
                MessageBox.Show("No se encontraron Postnets disponibles.", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void retrieveAllPostnet()
        {
            int nroSerie = obtenerNumerodeSerie();
            dsBoleto.PostnetDataTable dt = BizPostnet.getPostNet(txtMarca.Text, txtModelo.Text, nroSerie);
            if (dt.Count != 0)
            {
                this.dgvPostnet.DataSource = dt;
            }
            else if ((String.IsNullOrEmpty(txtMarca.Text)) && (String.IsNullOrEmpty(txtModelo.Text))
                && (String.IsNullOrEmpty(txtNroSerie.Text)))
            {
                MessageBox.Show("No existen registros en el sistema.", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("No existen registros para el criterio de busqueda ingresado.", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private int obtenerNumerodeSerie()
        {
            int nroSer;
            if (String.IsNullOrEmpty(txtNroSerie.Text))
            {
                nroSer = 0;
            }
            else
            {
                nroSer = Convert.ToInt32(txtNroSerie.Text);
            }
            return nroSer;
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            Dispose();
            Close();
        }

        private void btnAceptar_Click(object sender, EventArgs e)
        {
            List<DataGridViewRow> selectedList = new List<DataGridViewRow>();

            foreach(DataGridViewRow row in dgvPostnet.Rows)
            {
                DataGridViewCheckBoxCell cb = row.Cells[3] as DataGridViewCheckBoxCell;
                if (Convert.ToBoolean(cb.Value))
                {
                    selectedList.Add(row);
                }
            }
            selectManyPostNet(selectedList);
        }

        private void selectManyPostNet(List<DataGridViewRow> selectedList)
        {
            FormPrincipal form = ((FormPrincipal)this.MdiParent);
            if (form.MdiChildren.Any(f => f is FormAbmBeneficarios))
            {
                FormAbmBeneficarios frm = (FormAbmBeneficarios)form.MdiChildren.First(f => f is FormAbmBeneficarios);
                frm.setPostNetSelected(selectedList);
                Close();
            }
        }

        private void dgvPostnet_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
                OnDGWSelectedItem();
        }

        private void OnDGWSelectedItem()
        {
            dsBoleto.PostnetRow pr = (dsBoleto.PostnetRow)((DataRowView)(dgvPostnet.CurrentRow.DataBoundItem)).Row;
            switch (dgvPostnet.CurrentCell.ColumnIndex)
            {
                case 4://select
                    selectOnePostNet(pr);
                    break;

                default:
                    break;
            }
        }

        private void selectOnePostNet(dsBoleto.PostnetRow pr)
        {
            FormPrincipal form = ((FormPrincipal)this.MdiParent);
            if (form.MdiChildren.Any(f => f is FormBeneficiariosList))
            {
                FormBeneficiariosList frm = (FormBeneficiariosList)form.MdiChildren.First(f => f is FormBeneficiariosList);
                frm.setPostNetSelected(pr);
                Close();
            }
        }

        internal void setPostnetsToIgnore(List<int> idSelectedPostnet)
        {
            postnetsToIgnore = idSelectedPostnet;
        }

        private void txtNroSerie_KeyPress(object sender, KeyPressEventArgs e)
        {
            Util.limitarCampoNumericoEntero(e);
        }
    }
}
